﻿using System;
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using QFramework;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Gp.Scripts.GUI {
    public class ButtonGlowEffect : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler{
        private TweenerCore<Color, Color, ColorOptions> _tweenerCore; 
        
        [SerializeField] private Image glow;
        private Button _button;

        [SerializeField] private TMP_Text text;
        private Color _orgColor;

        private Color _orgText = Color.white;

        private void Start() {
            glow.ColorAlpha(0);
            _orgColor = glow.color;
            _button = GetComponent<Button>();

            if (text != null) _orgText = text.color;
        }
        

        public void OnPointerEnter(PointerEventData eventData) {
            if (_button == null || !_button.IsInteractable()) return;
            var temp = _orgColor;
            temp.a = 1;
            _tweenerCore = glow.DOColor(temp, 0.1f);

            if (text != null)
                text.color = Color.black;
        }
        

        public void OnPointerExit(PointerEventData eventData) {
            _tweenerCore?.Kill();
            glow.color = _orgColor;

            if (text != null) text.color = _orgText;
        }
    }
}